package com.jiuyin.dianjing.ui.fragment.first;

import android.util.Pair;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.jiuyin.dianjing.R;
import com.jiuyin.dianjing.api.interfaces.ApiResultCallback;
import com.jiuyin.dianjing.api.server.ServerApi;
import com.jiuyin.dianjing.app.HelperApplication;
import com.jiuyin.dianjing.model.GameInfoModel;
import com.jiuyin.dianjing.ui.fragment.base.BaseTabFragment;
import com.jiuyin.dianjing.ui.fragment.game.FragmentGame;
import com.jiuyin.dianjing.util.LogUtil;
import com.lzy.okgo.cache.CacheMode;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_GAME_ID;
import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_GAME_LIST;
import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_NAME;
import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_TOKEN;
import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_TYPE;
import static com.jiuyin.dianjing.api.constant.ApiConstant.VALUE_TYPE_HOT;
import static com.jiuyin.dianjing.api.enums.ApiEnum.APP_GET_GAME_API;


/**
 * 资讯（热门）
 */
public class FragmentFirstInnerFirst extends BaseTabFragment<GameInfoModel> {
    @BindView(R.id.tv_live)
    TextView mTextLive;

    @Override
    public void fetchData() {
        HashMap<String, Object> params = new HashMap<>();
        params.put(KEY_TOKEN, HelperApplication.getToken());
        params.put(KEY_TYPE, VALUE_TYPE_HOT);
        JSONObject jsonObject = new JSONObject(params);
        ServerApi.post(APP_GET_GAME_API.getUrl(), jsonObject, CacheMode.NO_CACHE, null, d -> addDisposable(d),
                new ApiResultCallback() {
                    @Override
                    public void onSuccess(JsonObject jsonObject) {
                        if (null != jsonObject && jsonObject.has(KEY_GAME_LIST)) {
                            LogUtil.log("FragmentFirstInnerFirst appGetGame onSuccess = " + jsonObject.toString());
                            JsonArray jsonArray = jsonObject.getAsJsonArray(KEY_GAME_LIST);
                            if (jsonArray != null) {
                                int size = jsonArray.size();
                                List<GameInfoModel> list = new ArrayList<>();
                                if (size > 0) {
                                    for (int i = 0; i < size; i++) {
                                        JsonObject object = jsonArray.get(i).getAsJsonObject();
                                        GameInfoModel gameInfoModel = new GameInfoModel();
                                        String name = object.get(KEY_NAME).getAsString();
                                        String game_id = object.get(KEY_GAME_ID).getAsString();
                                        gameInfoModel.setGameId(game_id);
                                        gameInfoModel.setName(name);
                                        list.add(gameInfoModel);
                                    }
                                    addData(list);
                                }
                            }
                        }
                    }

                    @Override
                    public void onFail(String msg) {
                        LogUtil.log(msg);
                    }

                    @Override
                    public void onError(String msg) {
                        LogUtil.log(msg);
                    }
                });
    }

    @Override
    protected int setLayoutId() {
        return R.layout.fragment_first_inner_first;
    }

    @Override
    protected void initView() {

    }

    @OnClick({R.id.tv_video, R.id.tv_live})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_video:
                break;
            case R.id.tv_live:
                initPopWindow();
                break;
        }
    }

    private void initPopWindow() {
        View popView = getLayoutInflater().inflate(R.layout.dialog_live, null);
        final PopupWindow popupWindow = new PopupWindow(popView,
                LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
        popupWindow.setOutsideTouchable(false);
        popupWindow.setFocusable(true);
        popupWindow.showAsDropDown(mTextLive);
        ImageView imageView = popView.findViewById(R.id.img_clear);
        imageView.setOnClickListener(v -> {
            popupWindow.dismiss();
        });
    }

    @Override
    protected void addFragments(List<GameInfoModel> list) {
        for (GameInfoModel model : list) {
            mFragments.add(Pair.create(model.getName(), FragmentGame.newInstance(model.getGameId(), VALUE_TYPE_HOT)));
        }
    }
}
